Skip to main content

content provider class

A Content Provider is a fundamental component in the Android framework that enables data sharing and data access between different applications. It acts as a mediator between applications and a structured data source, such as a SQLite database, file system, or even a remote server. Content Providers offer a standardized way to store, retrieve, and manipulate data, ensuring data integrity and security.

The primary purpose of a Content Provider is to manage access to a structured collection of data, which can be queried, inserted, updated, and deleted. It provides a consistent interface for other applications to interact with the data, regardless of the underlying data storage mechanism.

Here are some key concepts related to Content Providers:

  1. URI (Uniform Resource Identifier): A URI is used to identify and access a specific data set within a Content Provider. It follows the content://authority/path format, where the authority represents the Content Provider's unique identifier, and the path specifies the specific data set.

  2. MIME Types: Content Providers use MIME types to indicate the type of data they can handle. MIME types are typically represented as strings and help applications understand how to handle the data returned by a Content Provider. Common MIME types include vnd.android.cursor.dir (for a collection of data) and vnd.android.cursor.item (for a single item).

  3. CRUD Operations: Content Providers support the basic Create, Read, Update, and Delete (CRUD) operations. Other applications can perform these operations on the data provided by a Content Provider using appropriate methods.

  4. Data Access Permissions: Content Providers allow fine-grained control over data access permissions. Applications can specify permissions that restrict access to specific data sets, ensuring that only authorized applications can access or modify the data.

  5. Content Resolver: A Content Resolver is a client-side class that acts as an intermediary between applications and Content Providers. It enables applications to perform CRUD operations on the data exposed by Content Providers. Applications use the Content Resolver to query, insert, update, and delete data by providing the appropriate URIs and data.

Content Providers provide several benefits:

  1. Data Sharing: Content Providers enable different applications to share and access common data. This promotes data reusability and integration across applications.

  2. Data Abstraction: Content Providers abstract the underlying data storage mechanism from the client applications. Applications can access data from various sources (database, files, network) through a uniform interface, without needing to know the implementation details.

  3. Security and Permissions: Content Providers allow for fine-grained control over data access permissions. Applications can protect sensitive data and define access restrictions to ensure data security.

  4. Data Notifications: Content Providers can notify registered clients about changes in the data. This feature enables applications to stay updated with the latest changes in shared data.

Content Providers play a crucial role in enabling data sharing and interoperability in the Android ecosystem. They provide a standardized and secure way for applications to access and manipulate structured data, promoting modularity and reusability.

here is an example code of content provider:

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;

public class MyContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.example.myapp.provider";
private static final String BASE_PATH = "data";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

private static final int DATA = 1;
private static final int DATA_ID = 2;

private SQLiteOpenHelper dbHelper;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {
uriMatcher.addURI(AUTHORITY, BASE_PATH, DATA);
uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", DATA_ID);
}

@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext());
return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase database = dbHelper.getReadableDatabase();
Cursor cursor;

int match = uriMatcher.match(uri);
switch (match) {
case DATA:
cursor = database.query(MyDatabaseHelper.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
case DATA_ID:
selection = MyDatabaseHelper.ID_COLUMN + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
cursor = database.query(MyDatabaseHelper.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}

cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}

@Override
public String getType(Uri uri) {
int match = uriMatcher.match(uri);
switch (match) {
case DATA:
return "vnd.android.cursor.dir/" + AUTHORITY + "." + BASE_PATH;
case DATA_ID:
return "vnd.android.cursor.item/" + AUTHORITY + "." + BASE_PATH;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}

@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
long id = database.insert(MyDatabaseHelper.TABLE_NAME, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(CONTENT_URI, id);
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase database = dbHelper.getWritableDatabase();

int match = uriMatcher.match(uri);
int rowsUpdated;
switch (match) {
case DATA:
rowsUpdated = database.update(MyDatabaseHelper.TABLE_NAME, values, selection, selectionArgs);
break;
case DATA_ID:
selection = MyDatabaseHelper.ID_COLUMN + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
rowsUpdated = database.update(MyDatabaseHelper.TABLE_NAME, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}

if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}

return rowsUpdated;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase database = dbHelper.getWritableDatabase();

int match = uriMatcher.match(uri);
int rowsDeleted;
switch (match) {
case DATA:
rowsDeleted = database.delete(MyDatabaseHelper.TABLE_NAME, selection, selectionArgs);
break;
case DATA_ID:
selection = MyDatabaseHelper.ID_COLUMN + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
rowsDeleted = database.delete(MyDatabaseHelper.TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}

if (rowsDeleted != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}

return rowsDeleted;
}
}